package com.csii.itest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import com.csii.poi.excle.PdfBuilder;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.RectangleReadOnly;

//import com.aspose.cells.*;
import fr.opensagres.xdocreport.utils.StringUtils;

public class WordPdfUtils {
	static Rectangle layout2 = new RectangleReadOnly(595F, 842F);
	//protected static final Logger logger = LoggerFactory.getLogger(WordPdfUtils.class);
	
    public static void main(String[] args) throws Exception{
    	PDFAddWaterMark pdfwater = new PDFAddWaterMark();
    	
    	wordToPdf("C:\\Users\\wangjianghui\\Desktop\\测试pdf\\案例4.docx","C:\\Users\\wangjianghui\\Desktop\\测试pdf\\text.pdf");
//    	pdfwater.addPdfMark("d:\\2.pdf","d:\\21.pdf");
//        wordToPdf("d:\\1484279799209tongYeFinanceFiles.docx","d:\\1484279799209tongYeFinanceFiles.pdf");
//        wordToPdf("d:\\1481523801782tongYeFinanceFiles.docx","f:\\1481523801782tongYeFinanceFiles.pdf");
//        wordToPdf("d:\\1481875753224financeProdFiles.docx","f:\\1481875753224financeProdFiles.pdf");
////        wordToPdf("d:\\1481876412744financeProdFiles.docx","f:\\1481876412744financeProdFiles.pdf");
//        wordToPdf("d:\\csii20170406wangjianghui.docx","f:\\csii20170406wangjianghui.pdf");
//        wordToPdf("d:\\financeProdFiles.docx","f:\\financeProdFiles.pdf");
//        wordToPdf("d:\\priseManagementFiles.docx","f:\\priseManagementFiles.pdf");
//        wordToPdf("d:\\tongYeFinanceFiles.docx","f:\\tongYeFinanceFiles.pdf");
    }
//    public static void excel2pdf(String Address) {               
////    	if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生           
////    	return;       
////    	}       
//    	try {           
//    	File pdfFile = new File("C:/inetpub/wwwroot/web/file/pdf1.pdf");// 输出路径           
//    	Workbook wb = new Workbook(Address);// 原始excel路径                        
//    	FileOutputStream fileOS = new FileOutputStream(pdfFile);           
//    	wb.save(fileOS, SaveFormat.PDF);             
//    	fileOS.close();                 
//    	}
//    	catch (Exception e) {           
//    	e.printStackTrace();       
//    	}   
//    	}
    	
    
    
    
    /**
     * 将word文档， 转换成pdf
     * @param filepath 源文件路径， 必须为docx文档
     * @param outpath 目标文件路径
     * @param 
     * @throws Exception
     */
    public static void wordToPdf(String filepath,
			String outpath) throws Exception {
    	InputStream source = new FileInputStream(filepath);
        OutputStream target = new FileOutputStream(outpath);
        Map<String, String> params = new HashMap<String, String>();
        // 设置输出流格式
        PdfBuilder builder  = new PdfBuilder(target);
        // 设置页边距
//        builder.buildDocument()
//        .buildPageSetting(10, 10, 10, 10)//页边距
//        .buildPageSetting(layout2);//纸张大小
        PdfOptions options = PdfOptions.create();
        
        wordConverterToPdf(source, target, options, params);
	}
    

	/**
     * 将word文档， 转换成pdf, 中间替换掉变量
     * @param source 源为word文档， 必须为docx文档
     * @param target 目标输出
     * @param params 需要替换的变量
     * @throws Exception
     */
    public static void wordConverterToPdf(InputStream source,
			OutputStream target, Map<String, String> params) throws Exception {
		wordConverterToPdf(source, target, null, params);	
	}

	/**
     * 将word文档， 转换成pdf, 中间替换掉变量
     * @param source 源为word文档， 必须为docx文档
     * @param target 目标输出
     * @param params 需要替换的变量
     * @param options PdfOptions.create().fontEncoding( "windows-1250" ) 或者其他
     * @throws Exception
     */
    public static void wordConverterToPdf(InputStream source, OutputStream target, 
    		PdfOptions options,
    		Map<String, String> params) throws Exception {
    	 XWPFDocument doc = new XWPFDocument(source);
//         paragraphReplace(doc.getParagraphs(), params);
//         for (XWPFTable table : doc.getTables()) {
// 			for (XWPFTableRow row : table.getRows()) {             
// 				for (XWPFTableCell cell : row.getTableCells()) {
// 					paragraphReplace(cell.getParagraphs(), params);
// 				}
// 			}
// 		}
    	 
 		PdfConverter.getInstance().convert(doc, target, options);
 		
    }
    
    /** 替换段落中内容 */
	private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {
		if (MapUtils.isNotEmpty(params)) {
			for (XWPFParagraph p : paragraphs){
				for (XWPFRun r : p.getRuns()){
					String content = r.getText(r.getTextPosition());
					//logger.info(content);
					if(StringUtils.isNotEmpty(content) && params.containsKey(content)) {
						r.setText(params.get(content), 0);
					}
				}
			}
		}
	}
	
}
